namespace array_demo.Model
{
    public class LinkedList3<Key, Value>
    {
        /// <summary>
        /// 定义节点对象
        /// </summary>
        private class Node
        {
            // 当前的元素
            public Key key;

            public Value value;

            // 当前指向下一个节点的引用
            public Node next;

            /// <summary>
            ///初始化构造函数
            /// </summary>
            /// <param name="e"></param>
            /// <param name="next"></param>
            public Node(Key key, Value value, Node next)
            {
                this.key = key;
                this.value = value;
                this.next = next;
            }

            /// <summary>
            /// 初始化构造函数 不指定下一级别
            /// </summary>
            /// <param name="e"></param>
            public Node(Key key, Value value)
            {
                this.key = key;
                this.value = value;
                this.next = null;
            }

            public override string ToString()
            {
                return $"{key.ToString()}:{value.ToString()}";
            }
        }

        /// <summary>
        /// 头结点
        /// </summary>
        private Node head;


        /// <summary>
        /// 链表中的元素个数
        /// </summary>
        private int N;

        public LinkedList3()
        {
            head = null;
            N = 0;
        }

        public int Count
        {
            get { return N; }
        }

        public bool IsEmpty
        {
            get { return N == 0; }
        }

        public void AddNode(Key key, Value value)
        {
            Node node = GetNode(key);
            if (node == null)
            {
                head = new Node(key, value, head);
                N++;
            }
            else
            {
                node.value = value;
            }
        }

        public bool IsContains(Key key)
        {
            return GetNode(key) != null;
        }

        public Value GetValue(Key key)
        {
            Node node=GetNode(key);
            if(node==null)
                throw new ArgumentException("键不存在");
            else
              return node.value;
        }


        public void Set (Key key,Value value)
        {
             Node node=GetNode(key);
            if(node==null)
                throw new ArgumentException("键不存在");
            else
               node.value=value;
        }

        
        public void Remove(Key e)
        {
            Node curr = head;
            Node pre = null;

            if (curr.key.Equals(e))
            {
                head = head.next;
                N--;
            }
            else
            {
                while (curr != null)
                {
                    // 判断是否找到了值
                    if (curr.key.Equals(e))
                        break;
                    // 如果没有,移动到下一个节点并将值存在pre里面
                    pre=curr;
                    curr=curr.next;
                }

                // 说明找到了这个元素
               if(curr!=null)
               {
                   pre.next=pre.next.next;
                   N--;
               }
            }

        }

        private Node GetNode(Key key)
        {
            Node current = head;
            while (current != null)
            {
                if (current.key.Equals(key))
                    return current;
                current = current.next;
            }
            return null;
        }


    }
}